package com.purvar.doc.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.purvar.anotation.Log;
import com.purvar.constant.CommonConstant;
import com.purvar.core.bean.Response;
import com.purvar.doc.service.ISysDictItemService;
import com.purvar.plm.doc.entity.BaseAttribute;
import com.purvar.plm.doc.entity.SysDict;
import com.purvar.plm.doc.entity.SysDictItem;
import com.purvar.plm.doc.mapper.BaseAttributeMapper;
import com.purvar.plm.doc.mapper.SysDictMapper;
import com.purvar.util.SpringContextHolder;
import com.purvar.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author zcc
 * @version 1.0.0
 * @ClassName BaseAttributeMaterialController.java
 * @Description 物料属性
 * @createTime 2020-06-04
 */
@RestController
@RequestMapping("/api/DictController")
@Api(tags = "数据字典解析")
@Slf4j
public class DictController {

    @Log(value = "获取(物料属性)分页数据列表")
    @PostMapping(value = "/queryPageList")
    @ApiOperation(value = "获取(物料属性)分页数据列表", notes = "获取(物料属性)分页数据列表")
    public Response queryPageList() {
        String url = "https://list.szlcsc.com/catalog/312.html";

        Document document = null;
        try {
            document = Jsoup.connect(url).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = "";
        Element element = document.getElementsByTag("body").get(0);
        url();

        return Response.ok();
    }

    public static void main(String[] args) {
        String url = "https://list.szlcsc.com/catalog/312.html";

        Document document = null;
        try {
            document = Jsoup.connect(url).get();

            String result = "";
            Element element = document.getElementsByTag("body").get(0);

            List<Element> elements = element.getElementsByClass("select");

            for (int i = 0; i < elements.size(); i++) {
                //创建列表值，常见
                try {
                    Element h2 = elements.get(i).getElementsByTag("h2").get(0);
                    Element Ullist = elements.get(i).getElementsByTag("ul").select("li").get(0);
                    String text = h2.text();

                    List<Element> lilist = Ullist.children();
                    System.out.println(text);

                    elements.get(i).text();
                    for (Element element1 : lilist) {

                        System.out.println(element1.text());
                    }



                } catch (Exception e) {
                    e.printStackTrace();


                }
                System.out.println("**********************************");

            }


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void url() {
        String url = "https://list.szlcsc.com/catalog/312.html";

        Document document = null;
        try {
            document = Jsoup.connect(url).get();

            String result = "";
            Element element = document.getElementsByTag("body").get(0);

            List<Element> elements = element.getElementsByClass("select");

            for (int i = 0; i < elements.size(); i++) {
                //创建列表值，常见
                try {
                    Element h2 = elements.get(i).getElementsByTag("h2").get(0);
                    Element Ullist = elements.get(i).getElementsByTag("ul").get(0);
                    String text = h2.text();
                    //查询列表值是否存在
                    SysDictMapper sysDictMapper = SpringContextHolder.getBean(SysDictMapper.class);
                    QueryWrapper queryWrapper = new QueryWrapper();
                    queryWrapper.eq("name", text);
                    int con = sysDictMapper.selectCount(queryWrapper);
                    if (con > 0) {
                        continue;
                    }
                    //存入列表值
                    String maxcode = sysDictMapper.selectmaxcode();
                    int num = Integer.valueOf(maxcode) + 1;
                    String max = String.valueOf(num);
                    SysDict queryVo = new SysDict();
                    queryVo.setCode(max);
                    queryVo.setDelFlag(CommonConstant.DEL_FLAG_1);
                    if (StringUtils.isEmpty(queryVo.getType())) {
                        queryVo.setType("1");
                    }
                    queryVo.setName(text);
                    queryVo.setRemark(text);
                    queryVo.setCanDel("1");
                    queryVo.setStatus("1");
                    sysDictMapper.insert(queryVo);
                    List<Element> lilist = Ullist.getElementsByTag("li");
                    System.out.println(text);
                    List<SysDictItem> sysdictItems = new ArrayList<>();

                    for (Element element1 : lilist) {
                        SysDictItem sysDictItem = new SysDictItem();
                        sysDictItem.setCode(element1.text());
                        sysDictItem.setName(element1.text());
                        sysDictItem.setStatus("1");
                        sysDictItem.setDictId(queryVo.getId());
                        sysdictItems.add(sysDictItem);
                        System.out.println(element1.text());
                    }
                    ISysDictItemService iSysDictItemService=SpringContextHolder.getBean(ISysDictItemService.class);
                    iSysDictItemService.saveOrUpdateBatch(sysdictItems);
                    //同步属性数据
                    BaseAttribute attribute = new BaseAttribute();
                    attribute.setName(text);
                    attribute.setReadonly("1");
                    attribute.setBomView("1");
                    attribute.setCode(RandomUtil.randomString(6));
                    attribute.setStatus("1");
                    attribute.setNumberType("3");
                    attribute.setDictCode(max);

                    attribute.setRequired("0");
                    attribute.setAttributeType("2");
                    attribute.setType("1");
                    attribute.setSelectType("1");
                    attribute.setDateConditon("0");
                    BaseAttributeMapper baseAttributeMapper = SpringContextHolder.getBean(BaseAttributeMapper.class);
                    baseAttributeMapper.insert(attribute);


                } catch (Exception e) {
                    e.printStackTrace();


                }
                System.out.println("**********************************");

            }


        } catch (IOException e) {
            e.printStackTrace();
        }


    }




}
